package com.ruoyi.cm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.cm.domain.bo.CmBsProcessBo;
import com.ruoyi.cm.domain.vo.CmBsProcessVo;
import com.ruoyi.cm.domain.CmBsProcess;
import com.ruoyi.cm.mapper.CmBsProcessMapper;
import com.ruoyi.cm.service.ICmBsProcessService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工序Service业务层处理
 *
 * @author zxs
 * @date 2024-10-16
 */
@RequiredArgsConstructor
@Service
public class CmBsProcessServiceImpl implements ICmBsProcessService {

    private final CmBsProcessMapper baseMapper;

    /**
     * 查询工序
     */
    @Override
    public CmBsProcessVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工序列表
     */
    @Override
    public TableDataInfo<CmBsProcessVo> queryPageList(CmBsProcessBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmBsProcess> lqw = buildQueryWrapper(bo);
        Page<CmBsProcessVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<CmBsProcessVo> records = result.getRecords();
        for(CmBsProcessVo vo : records) {
            String p1 = vo.getPaymentRatio1();
            String p2 = vo.getPaymentRatio2();
            if (p1 == null && p2 == null) {

            } else {
                vo.setPaymentRatio(vo.getPaymentRatio1() + "~" + vo.getPaymentRatio2());
            }
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询工序列表
     */
    @Override
    public List<CmBsProcessVo> queryList(CmBsProcessBo bo) {
        LambdaQueryWrapper<CmBsProcess> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CmBsProcess> buildQueryWrapper(CmBsProcessBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmBsProcess> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), CmBsProcess::getName, bo.getName());
        lqw.eq(bo.getSort() != null, CmBsProcess::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getFileType()), CmBsProcess::getFileType, bo.getFileType());
        lqw.eq(bo.getEnabled() != null, CmBsProcess::getEnabled, bo.getEnabled());
        lqw.orderByAsc(CmBsProcess::getSort);
        return lqw;
    }

    /**
     * 新增工序
     */
    @Override
    public Boolean insertByBo(CmBsProcessBo bo) {
        CmBsProcess add = BeanUtil.toBean(bo, CmBsProcess.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工序
     */
    @Override
    public Boolean updateByBo(CmBsProcessBo bo) {
        CmBsProcess update = BeanUtil.toBean(bo, CmBsProcess.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmBsProcess entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工序
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
